package com.github.wxiaoqi.security.biz.api;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.biz.BhResumeBiz;
import com.github.wxiaoqi.security.biz.biz.BhTaskUserResumeBiz;
import com.github.wxiaoqi.security.biz.entity.BhResume;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("bhResume")
public class ResumeAction {
	@Autowired
	private BhResumeBiz bhResumeBiz;
	@Autowired
	private BhTaskUserResumeBiz bhTaskUserResumeBiz;
	/**
	 * 创建简历
	 */
	@RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addOrUpdateResume(@RequestBody Map<String, Object> req) {
		log.info("创建简历传入参数{}",req);
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String startWorkTimes = (String) req.get("startWorkTimes");
			String id = (String) req.get("id");
			DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); 
			if(startWorkTimes != null && !"".equals(startWorkTimes)){
				req.remove("endTimes");
				req.put("startWorkTime", format1.parse(startWorkTimes));
			}
			BhResume bhResume = EntityUtils.map2Bean(req, BhResume.class);
			if(null == id || "".equals(id)){
				log.info("创建{}",id);
				id = String.valueOf(SnowFlake.getId());
				bhResume.setId(id);
				bhResumeBiz.insert(bhResume);
			}else {
				log.info("编辑{}",id);
				bhResumeBiz.updateById(bhResume);
			}
			
			
			result.put("code", ResponseCode.OK.getCode());
			result.put("sucess", true);
			result.put("msg", ResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseCode.ABNORMAL_FIELDS.getCode());
			result.put("msg", ResponseCode.ABNORMAL_FIELDS.getMessage());
			result.put("sucess", false);
		}
		log.info("创建简历结束{}", result);
		return result;
	}
	/**
	 * 简历查看
	 */
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> infoResume(@RequestBody Map<String, Object> req) {
		log.info("查看简历传入参数{}", req);
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String id = (String) req.get("id");
			BhResume resume =  bhResumeBiz.selectById(id);
			result.put("code", ResponseCode.OK.getCode());
			result.put("sucess", true);
			result.put("msg", ResponseCode.OK.getMessage());
			result.put("body", resume);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseCode.ABNORMAL_FIELDS.getCode());
			result.put("msg", ResponseCode.ABNORMAL_FIELDS.getMessage());
			result.put("sucess", false);
		}
		log.info("查看简历结束{}", result);
		return result;
		
	}
	/**
	 * 投简历
	 * param:
	 * 任务编号taskId、用户编号custNo、简历编号resumeId
	 */
	@RequestMapping(value = "/send", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> sendResume(@RequestBody Map<String, Object> req) {
		log.info("投简历传入参数{}", req);
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result = bhTaskUserResumeBiz.sendResume(req);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseCode.ABNORMAL_FIELDS.getCode());
			result.put("msg", ResponseCode.ABNORMAL_FIELDS.getMessage());
			result.put("sucess", false);
		}
		log.info("投简历结束{}", result);
		return result;
	}
}
